package com.data.service;

import com.data.bean.CompanyMassage;
import com.data.bean.PersonMassage;
import com.data.bean.RegistMassage;
import com.data.dbdao.FillMassageDao;
import com.data.entity.ActiveEmail;
import com.data.entity.TempMassage;
import com.data.util.CheckDataUtils;
import com.data.util.FinalStringUtils;
import com.data.util.LoginMethodUtils;
import com.data.util.SendMailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Service
public class FillMassageService {
    @Autowired
    private FillMassageDao fillMassageDao;

    /**
     * 公司账号保存信息
     *
     * @param filename
     * @param massage
     * @param uid
     * @return
     */
    public boolean saveMassageCompanyService(MultipartFile filename, TempMassage massage, String uid) {
        //去掉首尾空格
        uid = uid.trim();

        CompanyMassage ms = new CompanyMassage();
        //id
        ms.setId(uid);
        //地址//
        ms.setAddr(massage.getAddr());
        //邮箱
        ms.setEmail(massage.getEmail());
        //手机号
        ms.setPhone(massage.getPhone());
        //官网
        ms.setUrl(massage.getUrl());
        //联系人
        ms.setName(massage.getCname());
        //职位
        ms.setWork(massage.getWork());
        ms.setEstate(FinalStringUtils.STATE_DEFAULT);
        ms.setCtime(new Date());
        String code = SendMailUtils.getActive(massage.getEmail());
        ms.setCode(code);
        RegistMassage rm = this.fileDetail(filename, uid);
        rm.setName(massage.getName());
        rm.setNumber(massage.getNumber());
        return fillMassageDao.saveMassageDao(ms, rm);
    }

    /**
     * 如果是个人身份
     *
     * @param filename
     * @param massage
     * @param uid
     * @return
     */
    public boolean saveMassagePersonService(MultipartFile filename, TempMassage massage, String uid) {
        //去掉首尾空格
        uid = uid.trim();
        PersonMassage ms = new PersonMassage();
        //id
        ms.setId(uid);
        //地址//
        ms.setAddr(massage.getAddr().trim());
        //邮箱
        ms.setEmail(massage.getEmail());
        //手机号
        ms.setPhone(massage.getPhone());
        //官网
        ms.setUrl(massage.getUrl());
        //邮箱状态
        ms.setEstate(FinalStringUtils.STATE_DEFAULT);

        RegistMassage rm = this.fileDetail(filename, uid);
        ms.setCtime(new Date());
        String code = SendMailUtils.getActive(massage.getEmail());
        ms.setCode(code);
        rm.setName(massage.getName());
        rm.setNumber(massage.getNumber());
        boolean bool = fillMassageDao.saveMassageDao(ms, rm);
        //发动激活邮件
        return bool && SendMailUtils.sendMailMassage(uid, massage.getEmail(), code, FinalStringUtils.PERSON);
    }

    /**
     * 注册时候提交的文件处里，就是身份证图片，或者营业执照
     *
     * @param filename
     * @param uid
     * @return
     */
    private RegistMassage fileDetail(MultipartFile filename, String uid) {
        RegistMassage rm = new RegistMassage();
        rm.setId(uid);
        rm.setState(FinalStringUtils.DEFAULT);
        try {
            rm.setNumpicture(filename.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            return rm;
        }
        return rm;
    }

    /**
     * 激活邮箱
     *
     * @param activeEmail
     * @return
     */
    public boolean activeEmailService(ActiveEmail activeEmail) {
        int type = activeEmail.getType();
        String id = activeEmail.getId();
        String email = activeEmail.getEmail();
        String code = activeEmail.getCode();

        return CheckDataUtils.stringUtils(id, email, code) && CheckDataUtils.checkAccountType(type) && fillMassageDao.changeAccountState(type, id, email, code);
    }

    /**
     * 选择注册账号的身份
     *
     * @param types
     * @return
     */
    public Boolean choiceAccountService(String id, int types) {
        id = id.trim();
        return CheckDataUtils.stringUtils(id) && CheckDataUtils.checkAccountType(types) && fillMassageDao.updateAccountTypeDao(id, types);
    }

    /**
     * 重发邮件的服务层信息
     *
     * @param id
     * @param email
     * @param type
     * @return
     */
    public boolean reSendMailService(String id, String email, int type) {

        if (checkMassage(id, email, type)) {
            id = id.trim();
            email = email.trim();
            String code = SendMailUtils.getActive(email);
            boolean bool = SendMailUtils.sendMailMassage(id, email, code, type);
            return bool && fillMassageDao.updateSendMailDao(id, email, type, code);
        }
        return false;

    }

    /***
     * 修改邮箱的服务层方法
     * @param id
     * @param email
     * @param type
     * @return
     */
    public boolean changeMailService(String id, String email, int type) {

        if (checkMassage(id, email, type)) {
            id = id.trim();
            email = id.trim();
            String code = SendMailUtils.getActive(email);
            boolean bool = SendMailUtils.sendMailMassage(id, email, code, type);
            return bool && fillMassageDao.changeMailDao(id, email, type, code);
        }
        return false;
    }

    private boolean checkMassage(String id, String email, int type) {
        //校验邮箱格式正确,身份是否是公司与个人两种状态
        return CheckDataUtils.stringUtils(id, email) && CheckDataUtils.checkEmail(email) && CheckDataUtils.checkAccountType(type);


    }

}
